import {ICBleSkipProtocol} from "./ICBleSkipProtocol.js";
import {ICCommon} from "./ICCommon.js";
import {ICStreamBuffer} from "./ICStreamBuffer.js";

var bledatas = [];
var protocolHandler = new ICBleSkipProtocol();

// let data = protocolHandler.WriteRespEncoder({
//     'device_type': 0x60,
//     'mode': 2,
//     'param': 50,
//     'time': (new Date()).getTime()
// });
// data = ICCommon.byte2hex(data[0]);
// console.log(protocolHandler.encode(data))
let cal = calcCalories1(14, 68);

function calcCalories1(time, weight) {
    if (time < 1 && weight < 0) {
        return 0
    }
    let nW = Math.floor(weight)
    let cal = (1000 * (time * (nW * 12))) / 3600
    // return cal.toFixed(2)
    return  Math.round(cal);
}

let data3 = protocolHandler.WriteRespEncoder1({device_type: 0x60}, 0xA1);
data3 = ICCommon.byte2hex(data3[0]);
// console.log("Recall Device Data " + data3);


var result = [
    // 'ac60a20063000000000000000000000000000005',
    // 'ac60a0010000000a000800000000000000003bee',
    // 'ac60a0010000000a000800000000000000003bee',

    'ac60a50200609f88100100000009000c500050f4',
    'ac60a5020100090009000c0000000000000000c6',
    'ac60a00300640000000001000000000000003f47',
];

result.forEach(function (item) {

    ICCommon.addArray(bledatas, ICCommon.hex2byte(item));
    var ret = protocolHandler.getProtocolData(bledatas);
    var code = ret[0];
    if (code == 3 || code == 0) {
        bledatas = []
    } else if (code == 1) {
        var data = ret[1];
        var decodeData = protocolHandler.decode(data, 0x00);
        // console.log('====')
        // // console.log(decodeData)
        // console.log(JSON.stringify(decodeData[0]));return;
        bledatas = []
        if (decodeData[0].package_type == 163) {
            let endTime = (new Date()).getTime()
            let totalTime = endTime - decodeData[0].time;
            let data = {
                type: 30001,
                startTime: decodeData[0].time,
                endTime: endTime,
                value: {
                    sportType: decodeData[0].mode,
                    totalCalories: calcCalories(decodeData[0].jumptime, 68),
                    chiefSportDataType: 5,
                    sportDataSource: 5,
                    totalTime: totalTime,
                    mExtendTrackDataMap: {
                        skipSpeed: decodeData[0].avg + '',
                        skipNum: decodeData[0].jump + '',
                        stumblingRope: 1,
                        maxSkippingTimes: '',
                    },
                }
            };
            console.log('upload data:' + JSON.stringify(data))
            // that.jsInjection.saveHealthData(JSON.stringify(data) , "saveHealthData");
        }
    } else {
        console.log('code:' + code)
    }
})

function calcCalories(time, weight) {
    if (time < 1 && weight < 0) {
        return 0
    }
    var nW = Math.floor(weight)
    var cal = (1000 * (time * (nW * 12))) / 3600 / 1000
    return cal.toFixed(2)
}

// ICCommon.addArray(bledatas, ICCommon.hex2byte('ac60a303005fb88d340303e8006200321e006987'));
// ICCommon.addArray(bledatas, ICCommon.hex2byte('ac60a303010015002500020001000b000b00403a'));
// ICCommon.addArray(bledatas, ICCommon.hex2byte('ac60a303020001000000000000000000000000a9'));
// ICCommon.addArray(bledatas, ICCommon.hex2byte('ac60a20063000000000000000000000000000005'));
//
// var ret = protocolHandler.getProtocolData(bledatas);
// var code = ret[0];
// if (code == 3 || code == 0) {
//     bledatas = []
// } else if (code == 1) {
//     var data = ret[1];
//     var decodeData = protocolHandler.decode(data, 0x00);
//     console.log(JSON.stringify(decodeData[0]))
// }
